package com.cpi.cornpp.grinddistributionversion.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.grinddistributionversion.converter.GrindDistributionVersionConverter;
import com.cpi.cornpp.grinddistributionversion.dao.GrindDistributionVersionDAO;
import com.cpi.cornpp.grinddistributionversion.dto.GrindDistributionPrioritiesVersionDTO;
import com.cpi.cornpp.grinddistributionversion.dto.GrindDistributionSettingsVersionDTO;
import com.cpi.cornpp.grinddistributionversion.exception.GrindDistributionVersionException;
import com.cpi.cornpp.grinddistributionversion.view.GrindDistributionVersionPlantGroupView;
import com.cpi.cornpp.grinddistributionversion.view.GrindDistributionVersionView;

/**
 * Grind Priority Distribution Version Service
 * @author Edgar Joao
 *
 */
public class GrindDistributionVersionService {

	public List getGrindDistributionVersion(int versionId, int periodId) throws GrindDistributionVersionException{
		GrindDistributionVersionDAO dao = null;
		GrindDistributionVersionConverter converter = null;
		List grindDistributionVersionList = null;
		List[] returnList = null;
		List settingsList = null;
		List priorityList = null;
		try{
			settingsList = new ArrayList(0);
			priorityList = new ArrayList(0);
			grindDistributionVersionList = new ArrayList(0);
			dao = new GrindDistributionVersionDAO();
			converter = new GrindDistributionVersionConverter();
			
			settingsList = dao.getGrindSettingList(versionId, periodId);	
			priorityList = dao.getGrindPrioritiesList(versionId, periodId);
			
			returnList = new List[]{settingsList, priorityList};			
			grindDistributionVersionList = converter.convertDTOsToPlantGroupView(returnList);
			
		} catch (GrindDistributionVersionException grindDistributionVersionException) {
			throw grindDistributionVersionException;
		} catch (Exception e){
			GrindDistributionVersionException grindDistributionVersionException = null;
			e.printStackTrace(System.out);
			grindDistributionVersionException = new GrindDistributionVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw grindDistributionVersionException;
		}
		return grindDistributionVersionList;
	}
	
	public void save(List grindDistributionVersionList)throws GrindDistributionVersionException{
		GrindDistributionVersionConverter grindDistributionVersionConverter = null;
		Iterator iterator = null;
		Iterator iteratorInside = null;
		GrindDistributionVersionPlantGroupView grindDistributionVersionPlantGroupView = null;
		List grindDistributionVersionViews = null;
		GrindDistributionVersionView grindDistributionVersionView = null;
		GrindDistributionPrioritiesVersionDTO grindDistributionPrioritiesVersionDTO = null;
		GrindDistributionSettingsVersionDTO grindDistributionSettingsVersionDTO = null;
		GrindDistributionVersionDAO grindDistributionVersionDAO = null;
		try{
			grindDistributionVersionConverter= new GrindDistributionVersionConverter();
			grindDistributionVersionDAO = new GrindDistributionVersionDAO();
			if(grindDistributionVersionList!=null){
				iterator = grindDistributionVersionList.iterator();
				GrindDistributionSettingsVersionDTO settingsVersionACTUALDTO = null;
				GrindDistributionSettingsVersionDTO settingsVersionPREVIOUSDTO = null;
				double sum = 0;
				while(iterator.hasNext()){
					grindDistributionVersionPlantGroupView = (GrindDistributionVersionPlantGroupView)iterator.next();
					grindDistributionVersionViews = grindDistributionVersionPlantGroupView.getGrindDistributionVersionCornTypeViews();
					iteratorInside = grindDistributionVersionViews.iterator();
					while(iteratorInside.hasNext()){
						grindDistributionVersionView = (GrindDistributionVersionView)iteratorInside.next();						
						grindDistributionPrioritiesVersionDTO = grindDistributionVersionConverter.convertViewToPrioritiesDTO(grindDistributionVersionView.getPlantVersionId().intValue(), grindDistributionVersionView);
						grindDistributionSettingsVersionDTO = grindDistributionVersionConverter.convertViewToSettingsDTO(grindDistributionVersionView.getPlantVersionId().intValue(), grindDistributionVersionView);						
						grindDistributionVersionDAO.save(grindDistributionPrioritiesVersionDTO, grindDistributionSettingsVersionDTO);
						
						settingsVersionPREVIOUSDTO = grindDistributionSettingsVersionDTO;						
						if(settingsVersionACTUALDTO != null){
							if(settingsVersionACTUALDTO.equals(settingsVersionPREVIOUSDTO)){
								sum += settingsVersionPREVIOUSDTO.getUnloadAmount();
							}else{
								grindDistributionVersionDAO.updateUnload(settingsVersionACTUALDTO.getPlantVersionId().intValue(), sum);
								sum = 0;							
								sum = settingsVersionPREVIOUSDTO.getUnloadAmount();							
							}
						}else if(null == settingsVersionACTUALDTO){
							sum += settingsVersionPREVIOUSDTO.getUnloadAmount();
						}
						settingsVersionACTUALDTO = settingsVersionPREVIOUSDTO;
					}
				}
			}
		}catch (GrindDistributionVersionException grindDistributionVersionException) {
			throw grindDistributionVersionException;
		}catch (Exception e){
			GrindDistributionVersionException grindDistributionVersionException = null;
			e.printStackTrace(System.out);
			grindDistributionVersionException = new GrindDistributionVersionException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw grindDistributionVersionException;
		}
	}
}
